# Maintainer: Robin Candau <antiz@archlinux.org>

pkgbase=systray-x
_pkgbase=SysTray-X
pkgname=('systray-x-common' 'systray-x-kde')
pkgver=0.9.7
pkgrel=1
pkgdesc="A system tray extension for Thunderbird 68+ (for X)"
arch=('loong64' 'x86_64')
url="https://github.com/Ximi1970/systray-x"
license=('MPL2')
depends=('qt5-base' 'thunderbird')
makedepends=('git' 'zip' 'libx11' 'knotifications5')
source=("${pkgbase}-${pkgver}.tar.gz::${url}/archive/${pkgver}.tar.gz"
	"Makefile.patch")
sha256sums=('251b742f0b27de3ba70976ff7bfcf1836a693e5d234ea6513a3f1e57bcf9108d'
            'dac5b7a4a1ad38ed9429d40636b59cb6932d6c7afd20e50609a73e2ff260316a')

prepare() {
	cd "${pkgbase}-${pkgver}"
	
	# Input the correct path to the binary in the json file
	sed -i 's/\/path\/to\/native-messaging\/app/\/usr\/bin/g' "app/${_pkgbase//-/_}.json"

	# Patch the Makefile to build both binaries into a separated directory
	patch -Np1 <"${srcdir}"/Makefile.patch
}

_build() {
	if [ "${1}" == "systray-x-common" ]; then
		make OPTIONS="DEFINES+=NO_KDE_INTEGRATION" PREFIX="${1}"
	else
		make PREFIX="${1}"
	fi
}

build() {
	cd "${pkgbase}-${pkgver}"
	_build systray-x-common
	_build systray-x-kde
}

package_systray-x-common() {
pkgdesc="A system tray extension for Thunderbird 68+ (for X) - Common version"
optdepends=('gnome-shell-extension-appindicator: Gnome integration')
provides=("${pkgbase}")
conflicts=("${pkgbase}")

	cd "${pkgbase}-${pkgver}"
	install -Dm 755 "app/systray-x-common-build/${_pkgbase}-app/${_pkgbase}" "${pkgdir}/usr/bin/${_pkgbase}"
	install -Dm 644 "app/${_pkgbase//-/_}.json" "${pkgdir}/usr/lib/mozilla/native-messaging-hosts/${_pkgbase//-/_}.json"
	install -Dm 644 "${pkgbase}@Ximi1970.xpi" "${pkgdir}/usr/lib/thunderbird/extensions/${pkgbase}@Ximi1970.xpi"
	install -Dm 644 README.md "${pkgdir}/usr/share/doc/${pkgbase}/README.md"
}

package_systray-x-kde() {
pkgdesc="A system tray extension for Thunderbird 68+ (for X) - KDE version"
depends=('knotifications5')
provides=("${pkgbase}")
conflicts=("${pkgbase}")

	cd "${pkgbase}-${pkgver}"
	install -Dm 755 "app/systray-x-kde-build/${_pkgbase}-app/${_pkgbase}" "${pkgdir}/usr/bin/${_pkgbase}"
	install -Dm 644 "app/${_pkgbase//-/_}.json" "${pkgdir}/usr/lib/mozilla/native-messaging-hosts/${_pkgbase//-/_}.json"
	install -Dm 644 "${pkgbase}@Ximi1970.xpi" "${pkgdir}/usr/lib/thunderbird/extensions/${pkgbase}@Ximi1970.xpi"
	install -Dm 644 README.md "${pkgdir}/usr/share/doc/${pkgbase}/README.md"
}
